package com.gzhryc.common;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 加载库文件工具
 * @author 张浩
 *
 */
public class JVMTools {

	private static volatile boolean _mainStart = false;
	private static MainWaitThread mainWaitThread;

	/**
	 * 加载动态链接库
	 * @param libName
	 * @param path	
	 * @return
	 */
	public static boolean loadLibrary(String libName,String path){
		boolean isWindow = isWindowOS();
		try{
			if (isWindow) {// Window 系统  
				System.loadLibrary(libName);
			}else {
				System.loadLibrary(libName);
			}
			return true;
		}catch(UnsatisfiedLinkError e){
			if(StringUtils.isBlank(path)){path = "./";}
			//获取java.library.path路径
			String libraryPath = System.getProperty("java.library.path");
			try {
				if(isWindow){
					Matcher mat = Pattern.compile("^([^;]+);.*$").matcher(libraryPath);
					if(mat.find()){
						if(mat.group(1).matches("^.*"+File.separator+"$")){
							libraryPath = mat.group(1);
						}else{
							libraryPath = mat.group(1)+File.separator;
						}
					}
					//拷贝Window版本的库文件到系统库目录中
					FileUtils.copyFileToDirectory(new File(path + libName+".dll"), new File(libraryPath));
				}else{
					Matcher mat = Pattern.compile("^([^:]+):.*$").matcher(libraryPath);
					if(mat.find()){
						if(mat.group(1).matches("^.*"+File.separator+"$")){
							libraryPath = mat.group(1);
						}else{
							libraryPath = mat.group(1)+File.separator;
						}
					}
					//拷贝Linux版本的库文件到系统库目录中
					FileUtils.copyFileToDirectory(new File(path + "lib"+libName+".so"), new File(libraryPath));
				}
				//第二次加载
				if (isWindow) {// Window 系统  
					System.loadLibrary(libName);
				}else {
					System.loadLibrary(libName);
				}
				return true;
			} catch (Exception E) {
				e.printStackTrace();
			}
		}
		return false;
	}
	
	public static boolean copyFileToBin(String filePath){
		try {
			String name = FilenameUtils.getName(filePath);
			String path = System.getProperty("java.home");
			File file = new File(filePath);
			if(file.exists()){
				FileUtils.copyFileToDirectory(file,new File(path +"/bin"));
			}else{
				InputStream inStream = Class.class.getResourceAsStream(filePath);
				File targetFile = new File(path +"/bin/"+name);
				if(!targetFile.exists()){
					targetFile.createNewFile();
				}
				FileUtils.copyInputStreamToFile(inStream, targetFile);
			}
			return true;
		} catch (IOException e) {
			e.printStackTrace();
		}
		return false;
	}
	
	public static boolean copyFileToLib(String filePath){
		try {
			String name = FilenameUtils.getName(filePath);
			String path = System.getProperty("java.home");
			File file = new File(filePath);
			if(file.exists()){
				FileUtils.copyFileToDirectory(file,new File(path +"/lib"));
			}else{
				InputStream inStream = Class.class.getResourceAsStream(filePath);
				File targetFile = new File(path +"/lib/"+name);
				if(!targetFile.exists()){
					targetFile.createNewFile();
				}
				FileUtils.copyInputStreamToFile(inStream, targetFile);
			}
			return true;
		} catch (IOException e) {
			e.printStackTrace();
		}
		return false;
	}

	/**
	 * 是否是Window系统
	 * @return
	 */
	public static boolean isWindowOS(){
		String osName = System.getProperty("os.name");  
		return osName.matches("^(?i)Windows.*$");
	}

	public static void mainStart(){
		mainStart(null,null);
	}

	public static void mainStart(MainInitial mainInitial){
		mainStart(null,mainInitial);
	}

	public static void mainStart(Long times,MainInitial mainInitial){
		_mainStart = true;
		mainWaitThread = new MainWaitThread(Thread.currentThread(),times,mainInitial);
		mainWaitThread.start();
	}

	public static void mainStop(){
		_mainStart = false;
	}

	public static interface MainInitial {

		public void run();
	}

	private static class MainWaitThread extends Thread{

		final Thread mainThread;

		MainInitial mainInitial;
		Long times = 30000L;		//默认30秒

		public MainWaitThread(Thread mainThread,Long times,MainInitial mainInitial){
			this.mainThread = mainThread;
			this.mainInitial = mainInitial;
			if(times != null && times > 0){
				this.times = times;
			}
		}

		public void run(){
			while (_mainStart){
				try{
					if(mainInitial != null){
						try {
							mainInitial.run();
						}catch (Exception e){}
					}
					Thread.sleep(times);	//每30秒检查一次
				}catch (Exception e){}
			}
		}
	}
}
